/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-4-12
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.sysmgr.menu;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dao.RecrodRowMapper;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;
import com.pearlsoft.webapp.core.web.session.SessionProfile;
import com.pearlsoft.webapp.core.web.util.HttpUtil;
import com.pearlsoft.webapp.sysmgr.menu.service.MenuRowMapperHelper;
import com.pearlsoft.webapp.sysmgr.menu.service.MenuService;

/**
 *描述： 
 *
 * 
 */
@QueryHandlerComponent(name = "MenuQueryHandler")
public class MenuQueryHandler extends AbsBaseQueryHandler{
	private final String generalFields=" MENU_ID, MENU_CODE, MENU_NAME, MENU_IMG_CLS,MENU_ORDER,MENU_TYPE,MENU_URL,MENU_PARENT_ID,C_STATUS,C_REMARK,MENU_HANDLER";
	private final RecrodRowMapper generalRowMapper=MenuRowMapperHelper.generalRowMapper();

	public void handler(Query query, DataSet dataSet) throws Exception {

	}
	
	public void getPageButtons(Query query, DataSet dataSet) throws Exception {
		String userId = HttpUtil.getCookieValueByName(this.getHttpServletRequest(), SessionProfile.COOKIE_USERID);
		String menuCode = (String)query.get("menuCode");
		MenuService menuService = this.getBean("menuService", MenuService.class);
		List<Record> list=menuService.getPageButtons(userId, menuCode);
		dataSet.setRecords(list);
	}
	
	
	public void getChildrenForTree(Query query, DataSet ds) throws Exception {
			JdbcTemplate jdbcTemplate = this.getBean("jdbcTemplate", JdbcTemplate.class);
			String id = (String) query.get("node");
			String sql = "SELECT * FROM SYS_MENU WHERE MENU_TYPE='1' and MENU_PARENT_ID=? ORDER BY MENU_ORDER";
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[] {id });
			for (Iterator<Map<String, Object>> iter = list.iterator(); iter.hasNext();) {
				Map<String, Object> map = iter.next();
				ds.newRecord();
				ds.setValue("id", map.get("MENU_ID"));
				ds.setValue("text", map.get("MENU_NAME"));
				ds.setValue("qtip", map.get("MENU_NAME"));
				ds.setValue("url", map.get("MENU_URL"));
				ds.setValue("expanded", false);
				ds.setValue("leaf", false);
			}
	}
	
	
	public void getChildrenForGrid(Query query, DataSet ds) throws Exception {
		MenuService menuService = this.getBean("menuService", MenuService.class);
		String parentId = (String) query.get("id");
		List<Record> list = menuService.getChildern(parentId);
		ds.setRecords(list);
	}
	
	public void queryMenuTreeInRole(Query query, DataSet ds) throws Exception{
		MenuService menuService = this.getBean("menuService", MenuService.class);
		String roleId = (String)query.get("roleId");
		if(roleId !=null ){
			Record record = menuService.queryMenuTreeInRole(roleId);
			if( record!=null){
				ds.addRecrod(record);
			}
		}
	}
	
	public void queryMenuTreeNotInRole(Query query, DataSet ds) throws Exception{
		MenuService menuService = this.getBean("menuService", MenuService.class);
		String roleId = (String)query.get("roleId");
		if(roleId !=null ){
			Record record = menuService.queryMenuTreeNotInRole(roleId);
			if( record!=null){
				ds.addRecrod(record);
			}		
		}
	}


	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		String menuId =(String) query.get("objId");
		MenuService menuService = this.getBean("menuService", MenuService.class);
		Record record = menuService.getById(menuId);
		ds.addRecrod(record);	
	}


	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		String searchValue = (String)query.get("searchValue") ;
		String MENU_ID =(String)query.get("id") ;

		MenuService menuService = this.getBean("menuService", MenuService.class);
		StringBuilder sqlbuffer = new StringBuilder();
		String sql="select "+generalFields+" from sys_menu";
		sqlbuffer.append(sql);
		if(StringUtils.isBlank(MENU_ID) ){
			sqlbuffer.append(" where (1=1)  ");
		}else{
			sqlbuffer.append(" where MENU_PARENT_ID='"+MENU_ID+"'");
		}
		if( !StringUtils.isBlank(searchValue) ){
			sqlbuffer.append(" and ( ");
			appendLike(sqlbuffer, "MENU_CODE", searchValue);
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "MENU_NAME", searchValue);
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "MENU_TYPE", searchValue);
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "MENU_URL", searchValue);
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "C_STATUS", searchValue);	
			sqlbuffer.append(" OR ");
			appendLike(sqlbuffer, "C_REMARK", searchValue);	
			sqlbuffer.append(" ) ");
		}
		sqlbuffer.append(" ORDER BY MENU_PARENT_ID,MENU_ORDER ");
		List<Record> list  = menuService.getBaseSimpleJdbcDao().query(sqlbuffer.toString(), generalRowMapper);
		ds.setRecords(list);
	}


	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		MenuService menuService = this.getBean("menuService", MenuService.class);
		StringBuilder sqlbuffer = new StringBuilder();
		String sql="select "+generalFields+" from sys_menu";
		String MENU_ID =(String)query.get("id") ;
		String MENU_CODE =(String)query.get("MENU_CODE") ;
		String MENU_NAME =(String)query.get("MENU_NAME") ;
		String MENU_TYPE =(String)query.get("MENU_TYPE") ;
		String MENU_URL =(String)query.get("MENU_URL") ;
		String C_STATUS =(String)query.get("C_STATUS") ;
		String C_REMARK =(String)query.get("C_REMARK") ;
		sqlbuffer.append(sql);
		if(StringUtils.isBlank(MENU_ID)){
			sqlbuffer.append(" where (1=1)  ");
		}else{
			sqlbuffer.append(" where MENU_PARENT_ID='"+MENU_ID+"'");
		}
		String likeSql = "";
		StringBuilder likeBuffer = new StringBuilder();
		appendLikeAnd(likeBuffer, "MENU_CODE", MENU_CODE);
		appendLikeAnd(likeBuffer,  "MENU_CODE", MENU_CODE);
		appendLikeAnd(likeBuffer,  "MENU_NAME", MENU_NAME);
		appendLikeAnd(likeBuffer,  "MENU_TYPE", MENU_TYPE);
		appendLikeAnd(likeBuffer, "MENU_URL", MENU_URL);
		appendLikeAnd(likeBuffer, "C_STATUS", C_STATUS);
		appendLikeAnd(likeBuffer,  "C_REMARK", C_REMARK);
		likeSql = likeBuffer.toString();
		if( likeSql.length()>0){
			likeSql= likeSql.substring(0,likeSql.length()-4);
			sqlbuffer.append(" and (").append(likeSql).append(") ");
		}
		sqlbuffer.append(" ORDER BY MENU_PARENT_ID,MENU_ORDER ");
		List<Record> list  = menuService.getBaseSimpleJdbcDao().query(sqlbuffer.toString(), generalRowMapper);
		ds.setRecords(list);
	}
	
}